/**
 * https://leetcode.cn/problems/daily-temperatures/submissions/589481452/
 * 739. 每日温度
 * medium 2024.12.26
 * 单调栈
 */

class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& temperatures) {
        stack<int> s;
        int n=temperatures.size();
        vector<int> ans(n,0);
        for(int i=temperatures.size()-1;i>=0;--i){
            int x=temperatures[i];
            while(!s.empty()&&x>=temperatures[s.top()])
                s.pop();
            ans[i]=s.empty()?0:s.top()-i;
            s.push(i);
        }
        return ans;
    }
};